package com.xialing.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xialing.weblog.common.domain.dos.VideoTagRelDO;

import java.util.List;

/**
 * @author: 夏翎
 * @url: www.xialing.com
 * @date: 2025-09-23
 * @description: 视频标签关联Mapper
 **/
public interface VideoTagRelMapper extends BaseMapper<VideoTagRelDO> {

    /**
     * 根据视频 ID 删除关联记录
     * @param videoId 视频ID
     * @return 删除结果
     */
    default int deleteByVideoId(Long videoId) {
        return delete(Wrappers.<VideoTagRelDO>lambdaQuery()
                .eq(VideoTagRelDO::getVideoId, videoId));
    }

    /**
     * 根据视频 ID 集合批量删除
     * @param videoIds 视频ID列表
     * @return 删除结果
     */
    default int deleteByVideoIds(List<Long> videoIds) {
        return delete(Wrappers.<VideoTagRelDO>lambdaQuery()
                .in(VideoTagRelDO::getVideoId, videoIds));
    }

    /**
     * 根据标签 ID 查询关联的视频 ID 集合
     * @param tagId 标签ID
     * @return 视频ID列表
     */
    default List<Long> selectVideoIdsByTagId(Long tagId) {
        // 构建查询条件
        LambdaQueryWrapper<VideoTagRelDO> wrapper = Wrappers.<VideoTagRelDO>lambdaQuery()
                .select(VideoTagRelDO::getVideoId)
                .eq(VideoTagRelDO::getTagId, tagId);

        return selectList(wrapper).stream()
                .map(VideoTagRelDO::getVideoId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 根据视频 ID 查询关联的标签 ID 集合
     * @param videoId 视频ID
     * @return 标签ID列表
     */
    default List<Long> selectTagIdsByVideoId(Long videoId) {
        // 构建查询条件
        LambdaQueryWrapper<VideoTagRelDO> wrapper = Wrappers.<VideoTagRelDO>lambdaQuery()
                .select(VideoTagRelDO::getTagId)
                .eq(VideoTagRelDO::getVideoId, videoId);

        return selectList(wrapper).stream()
                .map(VideoTagRelDO::getTagId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 根据标签 ID 统计关联的视频数量
     * @param tagId 标签ID
     * @return 视频数量
     */
    default Long countByTagId(Long tagId) {
        return selectCount(Wrappers.<VideoTagRelDO>lambdaQuery()
                .eq(VideoTagRelDO::getTagId, tagId));
    }

    /**
     * 根据标签 ID 删除关联记录
     * @param tagId 标签ID
     * @return 删除结果
     */
    default int deleteByTagId(Long tagId) {
        return delete(Wrappers.<VideoTagRelDO>lambdaQuery()
                .eq(VideoTagRelDO::getTagId, tagId));
    }
}

